package me.zzp.service;

import me.zzp.domain.Page;
import me.zzp.domain.Pageable;
import me.zzp.model.User;
import me.zzp.skeleton.UserSkeleton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zhangzepeng
 */
@Service
public class UserService {

    @Autowired
    private UserSkeleton userSkeleton;

    public User create(String name) {
        User user = User.of(name);
        User newUser = userSkeleton.create(user);
        return newUser == null? user: newUser;
    }

    public User update(User user) {
        if (userSkeleton.update(user.getId(), user) > 0) {
            return userSkeleton.one(user.getId());
        } else {
            return user;
        }
    }

    public int delete(User user) {
        return userSkeleton.destroy(user.getId(), user.getVersion());
    }

    public int deleteForce(User user) {
        return userSkeleton.destroy(user.getId());
    }

    public Page<User> all() {
        return userSkeleton.all(new Pageable().ascend("id"));
    }
}
